package com.cl.service.imp;

import com.cl.NotFoundExcepiton;
import com.cl.dao.TypeReporsitory;
import com.cl.po.Type;
import com.cl.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:Long
 * @Date:Created in 17:49 2018/7/2
 * @Description:
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    TypeReporsitory typeReporsitory;

    @Transactional
    @Override
    public Type saveType(Type type) {
        return typeReporsitory.save(type);
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        return typeReporsitory.getOne(id);
    }

    @Transactional
    @Override
    public Type updateType(Long id, Type type) {
        Type type1 = typeReporsitory.getOne(id);
        if (type1 == null){
            throw new NotFoundExcepiton("无此类型");
        }
        BeanUtils.copyProperties(type,type1);
        return typeReporsitory.save(type1);
    }

    @Override
    public Type getTypeByName(String name) {
        return typeReporsitory.findByName(name);
    }

    @Transactional
    @Override
    public void deleteType(Long id) {
       typeReporsitory.deleteById(id);
    }

    @Transactional
    @Override
    public Page<Type> getTypeList(Pageable pageable) {
        return typeReporsitory.findAll(pageable);
    }

    @Override
    public List<Type> listType() {
        return typeReporsitory.findAll();
    }
}
